var app = getApp();
var server = require('../../utils/server');
var ajaxUrl = require('../../utils/url.js');
Page({
  data: {
    classifies:[           
        ],
    classifyList:[           
        ],
    cart: {
      count: 0,
      total: 0,
      list: {}
    },
    showCartDetail: false
  },
  onLoad: function (options) {
    var self = this;
    self.fetchClassifyList();

  },
  fetchClassifyList:function(){
    var self = this;
    self.setData({
          classifyList: [],
          classifies:[]
        });
    wx.request({
          method:"GET",
          url:ajaxUrl.ajaxUrl()+"category",
          header: {
          'Authorization': ajaxUrl.getCiphertext()
          },
          success: function(res) {
            console.log(res.data)
             // var contentObj = [];
               if(res.data.code == 0){               
                  for(var i = 0;i < res.data.data.length;i++){                                     
                    var bpush = true;
                    if (i != 0)
                    {
                      var pri_class = self.data.classifyList;
                      for (var j = 0; j < pri_class.length; j++){
                      if(pri_class[j].id == "a"+res.data.data[i].category.parentCategory._id)
                        bpush = false;
                      }
                    }                    
                    if (bpush) {
                      self.data.classifyList.push({
                        id: "a"+res.data.data[i].category.parentCategory._id,
                        classifyName:res.data.data[i].category.parentCategory.name,
                        classifies:res.data.data[i].category.parentCategory.subCategories
                      });
                    }
                    if (!res.data.data[i].category.icon)
                      res.data.data[i].category.icon = "/image/easygo.png";
                    self.data.classifies.push({
                        id:res.data.data[i].category._id,
                        name:res.data.data[i].category.name,
                        pic:res.data.data[i].category.icon
                    });
                  }
               }
               self.setData({
                 classifyList:self.data.classifyList,
                 classifies:self.data.classifies
               })
               if (!self.data.classifySeleted)
               {
                  self.setData({
                      classifySeleted:self.data.classifyList[0].id
                  }); 
               }
              //   self.data.postsList = contentObj
               setTimeout(function () {
                  self.setData({
                    hidden: true
                  });
              }, 300);
          }
      })
  },
  redictGoods: function(e){
    var categoryid =  e.currentTarget.dataset.categoryid;
    console.log("categoryid:"+categoryid);
    var link = "../goods/goods?categoryid="+categoryid;
    wx.navigateTo({
        url: link
      });
  },
  onShow: function () {
  },  
  onGoodsScroll: function (e) {
    if (e.detail.scrollTop > 10 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 10 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }

    var scale = e.detail.scrollWidth / 570,
      scrollTop = e.detail.scrollTop / scale,
      h = 0,
      classifySeleted,
      len = this.data.classifyList.length;
    this.data.classifyList.forEach(function (classify, i) {
      var height_num = classify.classifies.length / 3;
      var _h = 30 +  height_num * (160);
      if (scrollTop >= h ) {
        classifySeleted = classify.id;
      }
      h += _h;
    });
    this.setData({
      classifySeleted: classifySeleted
    });
  },
  tapClassify: function (e) {
    var id = e.currentTarget.dataset.id;         
    var self = this;
     self.setData({
        classifyViewed: id
    }); 

    setTimeout(function () {
      self.setData({
        classifySeleted: id
      });
    }, 200);     
  },
  onShareAppMessage: function () {
    return {
      title: '恒泰云跨境购',
      desc: '我的分类',
      path: '/pages/index/index'
    };
  }
});

